/**
 * @file Util.h
 *
 * This file defines global utilities.
 *
 * @author Luc Fourestier
 * @date 2010
 */

#ifndef UTIL_H_
#define UTIL_H_

#include <endian.h>

// Byte manipulation
#if 0
#define GET_U8(b, i)  	((unsigned char)b[i])
#define GET_U16(b, i) 	((((unsigned short )b[i]) << 8) + ((unsigned short )b[i+1]))
#define GET_U32(b, i) 	((((unsigned int )b[i]) << 24) + (((unsigned int )b[i+1]) << 16) + \
							(((unsigned int )b[i+2]) << 8) + ((unsigned int )b[i+3]))
#define GET_U64(b, i) 	((((unsigned long long )b[i]) << 56) + (((unsigned long long )b[i+1]) << 48) + (((unsigned long long )b[i+2]) << 40) + (((unsigned long long )b[i+3]) << 32) + \
							(((unsigned long long )b[i+4]) << 24) + (((unsigned long long )b[i+5]) << 16) + (((unsigned long long )b[i+6]) << 8) + ((unsigned long long )b[i+7]))
#else
#define GET_U8(b, i)  	((unsigned char)b[i])
#define GET_U16(b, i) 	be16toh(*((unsigned short*)(b+i)))
#define GET_U32(b, i) 	be32toh(*((unsigned int*)(b+i)))
#define GET_U64(b, i) 	be64toh(*((unsigned long long*)(b+i)))
#endif

// Time conversion
#define TIMEVAL_TO_SECONDS(x) ((double)((x)->tv_sec) + ((double)((x)->tv_usec)/1000000))
#define TIMEVAL_TO_USECONDS(x) (((unsigned long long)((x)->tv_sec) * 1000000) + (unsigned long long)((x)->tv_usec))

#endif /* UTIL_H_ */
